package dk.hewison.client.slideshow;

import dk.hewison.client.AppEvent;
import dk.hewison.client.domain.Directory;
import dk.hewison.client.domain.Slideshow;
import dk.hewison.client.menu.MainMenuController;
import dk.hewison.client.mvc.AbstractAction;
import dk.hewison.client.mvc.AbstractController;
import dk.hewison.client.mvc.Action;
import dk.hewison.client.mvc.View;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-15 12:47:07 +0000 (Sun, 15 Feb 2009) $:
 * @version $Revision: 364 $:
 */

public class SlideshowPageController extends AbstractController<Slideshow> {

    SlideshowView slideshowView;

    public SlideshowPageController(int id) {
        super(id);

        registerEvent(AppEvent.Event.START_SLIDESHOW);
    }

    @Override
    protected Action[] initActions() {
        Action pause = new AbstractAction(this, 1, "||") {
            @Override
            public void doExecute() {
                if (getText().equals("||")) {
                    slideshowView.pause();
                    setText("->");
                } else {
                    slideshowView.play();
                    setText("||");
                }
            }
        };
        Action exit = new AbstractAction(this, 5, "Exit") {
            @Override
            public void doExecute() {
                fireEvent(new AppEvent<Integer>(AppEvent.Event.PAGE_SELECTED, MainMenuController.ALL_ALBUMS));
                slideshowView.exit();
            }
        };

        return new Action[]{pause, exit};
    }

    public void eventOccurred(AppEvent appEvent) {
        switch (appEvent.getEvent()) {
            case START_SLIDESHOW:
                fireEvent(new AppEvent<Integer>(AppEvent.Event.PAGE_SELECTED, MainMenuController.SLIDESHOW));
                Directory directory = (Directory) appEvent.getObject();
                setModel(new Slideshow(directory));
                startSlideshow();
                //Window.alert("Start slideshow");
                break;
        }
    }

    public void startSlideshow() {
        slideshowView.startSlideshow();
    }

    @Override
    public boolean leavePage() {
        slideshowView.stopSlideshow();
        return true;
    }

    public void setModel(Slideshow directoryList) {
        view.setModel(directoryList);
    }

    public Slideshow getModel() {
        return null;
    }

    public View<Slideshow> createView() {
        slideshowView = new SlideshowView();
        return slideshowView;
    }
}